﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Regions;

namespace Tww.Paf.Api.RegionAdapter
{
    public class ContentPresenterRegionAdapter : RegionAdapterBase<ContentPresenter>
    {
        #region  - Konstruktoren -

        public ContentPresenterRegionAdapter(IRegionBehaviorFactory behaviorFactory)
            : base(behaviorFactory)
        {
        }

        #endregion

        #region  - Methoden (privat) -

        protected override void Adapt(IRegion region, ContentPresenter regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var element in e.NewItems.OfType<FrameworkElement>())
                    {
                        regionTarget.Content = element;
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (FrameworkElement currentElement in e.OldItems)
                        regionTarget.Content = null;
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }

        #endregion
    }
}
